/* ----------------------------------------------------------------------
*
* Copyright (C) 2025 XiaoFei. All rights reserved.
*
* $file:		XiaoZhi_ESP8266.h
* $Date:        18-August-2025
* $Revision: 	V1
* $Project: 	XiaoFei BSP Library
* $Taobao:      https://item.taobao.com/item.htm?abbucket=17&id=964738686451
*
* -------------------------------------------------------------------- */

#ifndef __XiaoZhi_ESP8266_H
#define __XiaoZhi_ESP8266_H
#define AD9834_PS(x)                    GPIO_WriteBit(AD9834_Port_PS,AD9834_Pin_PS,(BitAction)(x))
#define ESP8266_RCC_Usart(x)            RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, (FunctionalState)x)
#define ESP8266_RCC_TX(x)               RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, (FunctionalState)x)
#define ESP8266_RCC_RX(x)               RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, (FunctionalState)x)

#define ESP8266_TX_Pin                  GPIO_Pin_9
#define ESP8266_TX_GPIO                 GPIOA
#define ESP8266_RX_Pin                  GPIO_Pin_10
#define ESP8266_RX_GPIO                 GPIOA
#define ESP8266_USART                   USART1
#define ESP8266_BandRate                115200
#define ESP8266_Group                   NVIC_PriorityGroup_2
#define ESP8266_IRQn                    USART1_IRQn
#define ESP8266_IRQHandler              USART1_IRQHandler
#define ESP8266_ITPre                   1
#define ESP8266_ITSub                   1

#define ESP8266_TXMax                   512
#define ESP8266_RXMax                   1024

#ifndef ESP8266_BufDef_def
#define ESP8266_BufDef_def
typedef struct{
	char RXData[ESP8266_RXMax]; /*!< The receiving buffer of the ESP8266 */
    char TXData0[ESP8266_TXMax];/*!< The sending buffer of the ESP8266 */
	char TXData1[ESP8266_TXMax];/*!< The sending buffer of the ESP8266 */
	unsigned int Len;           /*!< Length of data received by the ESP8266 */
	char flag;                  /*!< ESP8266 data reception complete indicator */
}ESP8266_BufDef;
#endif

#ifndef ESP8266_WifiDef_def
#define ESP8266_WifiDef_def
typedef struct{
	char* Name;                 /*!< The name of the ESP8266 wifi */
	char* Password;             /*!< The Password of the ESP8266 wifi */
}ESP8266_WifiDef;
#endif

#ifndef ESP8266_IPDef_def
#define ESP8266_IPDef_def
typedef struct{
	char* IP;                   /*!< The IP to ESP8266 */
	char* Port;                 /*!< The Port to ESP8266 */
}ESP8266_IPDef;
#endif

#ifndef ESP8266_APModeDef_def
#define ESP8266_APModeDef_def
typedef enum{
	OPEN = 0,                   /*!< No encryption processing */
	WEP = 1,                    /*!< WEP encryption method */
	WPA_PSK = 2,                /*!< WPA_PSK encryption method */
	WPA2_PSK = 3,               /*!< WPA2_PSK encryption method */
	WPA_WPA2_PSK = 4,           /*!< WPA_WPA2_PSK encryption method */
}ESP8266_APModeDef;
#endif

#ifndef ESP8266_MQTTDef_def
#define ESP8266_MQTTDef_def
typedef struct{
	char* Address;              /*!< The destination address of the MQTT platform */
	char* port;                 /*!< The port of the MQTT platform */
	char* ClientID;             /*!< The MQTT ClientID */
	char* UserName;             /*!< The MQTT UserName */
	char* Password;             /*!< The MQTT Password */
	char* SubTopic;             /*!< The MQTT SubTopic */
	char* PubTopic;             /*!< The MQTT PubTopic */
}ESP8266_MQTTDef;
#endif

/* 
* @function: 	ESP8266_Init
*/
void ESP8266_Init(void);

/* 
* @function: 	ESP8266_Printf
* @parameter:	[in] format					 The parameter will be send in ESP8266_Printf.
*/
void ESP8266_Printf(char *format, ...);

/* 
* @function: 	ESP8266_Scanf
* @parameter:	[in] ms					     The timeout for data retrieval.
* @parameter:	[in] hand					 Header of the frame from which data needs to be retrieved.
* @parameter:	[in] format					 The parameter will be get from ESP8266_Scanf.
* @return:		Return 0x00 on error, otherwise return 0x01 plus the number of correctly retrieved data items.
*/
unsigned char ESP8266_Scanf(unsigned int ms, char* hand, char* format, ...);

/* 
* @function: 	ESP8266_ATTest
* @parameter:	[in] ms					     The timeout for data retrieval.
* @return:		Return 0x00 on error, otherwise return 0x01.
*/
unsigned char ESP8266_ATTest(unsigned int ms);

/* 
* @function: 	ESP8266_TCPClient
* @parameter:	[in] WIFI					 The information of the WIFI you are going to connect to.
* @parameter:	[in] IP					     The information of the IP you are going to connect to.
*/
void ESP8266_TCPClient(ESP8266_WifiDef WIFI, ESP8266_IPDef IP);

/* 
* @function: 	ESP8266_TCPClient_Printf
* @parameter:	[in] format					 The parameter will be send in ESP8266_TCPClient_Printf.
*/
void ESP8266_TCPClient_Printf(char *format, ...);

/* 
* @function: 	ESP8266_TCPClient_Scanf
* @parameter:	[in] ms					     The timeout for data retrieval.
* @parameter:	[in] format					 The parameter will be get from ESP8266_TCPClient_Scanf.
* @return:		Return 0x3F for incorrect statements, otherwise return the correct number of obtained data.
*/
unsigned char ESP8266_TCPClient_Scanf(unsigned int ms, char* format, ...);

/* 
* @function: 	ESP8266_TCPServer
* @parameter:	[in] WIFI					 The WIFI information to be created by the ESP8266.
* @parameter:	[in] IP					     The IP information to be created by the ESP8266.
* @parameter:	[in] APMode					 The encryption method of the WIFI to ESP8266.
* @parameter:	[in] channel				 The channel num of the WIFI to ESP8266.
* @parameter:	[in] overtime				 The server WIFI timeout for ESP8266.
*/
void ESP8266_TCPServer(ESP8266_WifiDef WIFI, ESP8266_IPDef IP, ESP8266_APModeDef APMode, int channel, int overtime);

/* 
* @function: 	ESP8266_TCPServer_Scanf
* @parameter:	[in] port					 The port where your data from.
* @parameter:	[in] ms					     The timeout for data retrieval.
* @parameter:	[in] format					 The parameter will be get from ESP8266_TCPServer_Scanf.
* @return:		Return 0x3F for incorrect statements, otherwise return the correct number of obtained data.
*/
unsigned char ESP8266_TCPServer_Scanf(char* port, unsigned int ms, char* format, ...);

/* 
* @function: 	ESP8266_TCPServer_Printf
* @parameter:	[in] port					 The port where your data will be send.
* @parameter:	[in] format					 The parameter will be send in ESP8266_TCPServer_Printf.
*/
void ESP8266_TCPServer_Printf(int port, char *format, ...);

/* 
* @function: 	ESP8266_UDP
* @parameter:	[in] WIFI					 The information of the WIFI you are going to connect to.
* @parameter:	[in] IP					     The information of the IP you are going to connect to.
*/
void ESP8266_UDP(ESP8266_WifiDef WIFI, ESP8266_IPDef IP);

/* 
* @function: 	ESP8266_UDP_Printf
* @parameter:	[in] format					 The parameter will be send in ESP8266_UDP_Printf.
*/
void ESP8266_UDP_Printf(char *format, ...);

/* 
* @function: 	ESP8266_UDP_Scanf
* @parameter:	[in] ms					     The timeout for data retrieval.
* @parameter:	[in] format					 The parameter will be get from ESP8266_UDP_Scanf.
* @return:		Return 0x3F for incorrect statements, otherwise return the correct number of obtained data.
*/
unsigned char ESP8266_UDP_Scanf(unsigned int ms, char* format, ...);

/* 
* @function: 	ESP8266_MQTT
* @parameter:	[in] WIFI					 The information of the WIFI you are going to connect to.
* @parameter:	[in] MQTT					 The information of your MQTT.
*/
void ESP8266_MQTT(ESP8266_WifiDef WIFI, ESP8266_MQTTDef MQTT);

/* 
* @function: 	ESP8266_MQTT_Printf
* @parameter:	[in] MQTT					 The handle of the MQTT message.
* @parameter:	[in] json					 The message will be send from ESP8266 by MQTT.
*/
void ESP8266_MQTT_Printf(ESP8266_MQTTDef MQTT, char *json);

/* 
* @function: 	ESP8266_MQTT_Scanf
* @parameter:	[in] MQTT					 The handle of the MQTT message.
* @parameter:	[in] json					 The message will be recrive from ESP8266 by MQTT.
* @return:		Return 0x00 if have no data is receive, otherwise return the length of json.
*/
int ESP8266_MQTT_Scanf(ESP8266_MQTTDef MQTT, char *json);

/* 
* @function: 	ESP8266_IRQHandler
* @callback:    The function will be callback when the ESP8266 receive the data.
*/
void ESP8266_IRQHandler(void)

#endif

